/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.guide.service.MobileIndexService;
import com.jeeplus.modules.guide.service.MobileSilentService;
import com.jeeplus.modules.guide.service.VersionService;
import com.jeeplus.modules.guide.utils.HttpRequestUtil;
import com.jeeplus.modules.guide.utils.MatterUtils;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 网办业务Controller
 * @author azj
 * @version 2021-5-15
 */
@RestController
@RequestMapping("/mobileToDo")
@RequiredArgsConstructor
public class MobileToDoController extends BaseController {
	private final MobileIndexService mobileIndexService;
	private final MatterUtils matterUtils;
	private final VersionService versionService;
	private final UserService userService;
	private final MobileSilentService mobileSilentService;
	//室内地址获取接口
	@Value("${snUrl}")
	private String snUrl;

	/**
	 * isHaveDoUrl 是否有网上办地址
	 * oid 一页一证id
	 * ids 情形化选择id集合
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="isHaveDoUrl")
	public AjaxJson getThingUrl(String oid, String titleIds, String valIds,String isThing) {
		String code,result;
		if("true".equals(isThing)){
			if(StringUtils.isNotBlank(titleIds)){
				titleIds = titleIds.substring(0,titleIds.length()-1);
			}
			code = String.valueOf(matterUtils.getThingUrl(oid,titleIds,valIds).get("code"));
			if(MatterUtils.SUCCESSCODE.equals(code)){
				return AjaxJson.success("加载成功！").put("isDo",true);
			}
		}else{
			if(oid.contains("street")){
				return AjaxJson.success("加载成功！").put("isDo",mobileIndexService.checkOnline(oid,versionService.getVersion()));
			}else{
				result = mobileIndexService.findIsHaveDo(versionService.getVersion(),oid);
				if(StringUtils.isNotBlank(result)){
					return AjaxJson.success("加载成功！").put("isDo",true);
				}else{
					result = mobileIndexService.findOtherUrl(oid,versionService.getVersion());
					if(StringUtils.isNotBlank(result)){
						return AjaxJson.success("加载成功！").put("isDo",true).put("isOther",true).put("url",result);
					}
				}
			}
		}
		return AjaxJson.success("暂无网上办数据！").put("isDo",false);
	}

	/**
	 * getThingUrl 获取一页一证网办地址
	 * oid 一页一证id
	 * ids 情形化选择id集合
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getThingUrl")
	public AjaxJson getThingUrl(String oid, String titleIds, String valIds) {
		if(StringUtils.isNotBlank(titleIds)){
			titleIds = titleIds.substring(0,titleIds.length()-1);
		}
		return matterUtils.getThingUrl(oid,titleIds,valIds);
	}

	/**
	 * 获取事项网上办理集合
	 *  * oid事项id
	 * getMatterTypeValue
	 * @return 受影响的行数
	 */
	@GetMapping(value ="getMatterTypeValue")
	public AjaxJson getMatterTypeValue (String oid, String titleIds, String valIds){
		if(StringUtils.isNotBlank(titleIds)){
			titleIds = titleIds.substring(0,titleIds.length()-1);
		}
		if(oid.contains("street")){
			List<Map<String,String>> list = mobileIndexService.getMatterTypeValue(oid,versionService.getVersion());
			return AjaxJson.success().put("data",list);
		}else{
			return matterUtils.getMatterUrl(oid,titleIds,valIds);
		}

	}

	/**
	 * 获取事项网上办理集合 添加用户单点信息
	 *  * oid事项id
	 * getMatterTypeValueByUser
	 * @return 受影响的行数
	 */
	@GetMapping(value ="getMatterTypeValueByUser")
	public AjaxJson getMatterTypeValueByUser (String oid, String titleIds, String valIds){
		if(StringUtils.isNotBlank(titleIds)){
			titleIds = titleIds.substring(0,titleIds.length()-1);
		}
		if(oid.contains("street")){
			if(UserUtils.getUser() == null || StringUtils.isBlank(UserUtils.getUser().getLoginName()) ){
				return AjaxJson.error("get user is null").put("code",400);
			}else{
				Map<String, String> user = mobileIndexService.findStreetUser(UserUtils.getUser().getLoginName());
				if(null == user || null == user.get("idcard")){
					return AjaxJson.error("get user is null").put("code",400);
				}
				List<Map<String,String>> list = mobileIndexService.getMatterTypeValue(oid,versionService.getVersion());
				for(Map<String,String> map : list){
					if(map.containsKey("address") && (map.get("address").contains("zdkc.cdgxxjh.cn") || map.get("address").contains("www.fcjjdbsc.com"))){
						map.put("address",map.get("address")+"&isGx=1&userid="+UserUtils.getUser().getLoginName()+"&tell="+user.get("tell")+"&username="+user.get("name")+"&idcard="+user.get("idcard"));
					}
				}
				return AjaxJson.success().put("data",list);
			}
		}else{
			return matterUtils.getMatterUrl(oid,titleIds,valIds);
		}
	}

	/**
	 * 获取事项网上办理集合 添加用户单点信息
	 *  * oid事项id
	 * getMatterTypeValueByUser
	 * @return 受影响的行数
	 */
	@GetMapping(value ="getStreetOnline")
	public AjaxJson getStreetOnline (String ids){
			if(UserUtils.getUser() == null || StringUtils.isBlank(UserUtils.getUser().getLoginName()) ){
				return AjaxJson.error("get user is null").put("code",400);
			}else{
				Map<String, String> user = mobileIndexService.findStreetUser(UserUtils.getUser().getLoginName());
				if(null == user || null == user.get("idcard")){
					return AjaxJson.error("get user is null").put("code",400);
				}
				String[] split = ids.split(",");
				if(split.length <= 0 ){
					return AjaxJson.error("参数无效");
				}
				List<Map<String,String>> list = mobileIndexService.getMatterOlineByOid(versionService.getVersion(),Arrays.asList(split));
				for(Map<String,String> map : list){
					if(map.containsKey("address") && (map.get("address").contains("zdkc.cdgxxjh.cn") || map.get("address").contains("www.fcjjdbsc.com"))){
						map.put("address",map.get("address")+"&isGx=1&userid="+UserUtils.getUser().getLoginName()+"&tell="+user.get("tell")+"&username="+user.get("name")+"&idcard="+user.get("idcard"));
					}
				}
				return AjaxJson.success().put("data",list);
			}
	}
	/**
	 * getFinshedBy 获取第三方平台办结集合
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getFinshed")
	public AjaxJson getFinshed() {
		User user = UserUtils.getUser();
		if(user == null || StringUtils.isBlank(user.getLoginName()) ){
			return AjaxJson.error("get user is null").put("code",400);
		}else{
			return matterUtils.getFinshed(user.getLoginName(),userService.getUserIdcard(user.getLoginName()));
		}
	}

	/**
	 * getunFinshedBy 获取第三方平台未办结集合
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getunFinshed")
	public AjaxJson getunFinshed() {
		User user = UserUtils.getUser();
		if(user == null || StringUtils.isBlank(user.getLoginName()) ){
			return AjaxJson.error("get user is null").put("code",400);
		}else{
			return matterUtils.getunFinshed(user.getLoginName(),userService.getUserIdcard(user.getLoginName()));
		}
	}

	/**
	 * getunFinshedBy 获取第三方平台未办结集合
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="delGXTask")
	public AjaxJson delGXTask(String id,String reason,String name) {
		return matterUtils.delGXTask(id,reason,name);
	}

	/**
	 * getunFinshedBy 获取排队数据信息通过排队编号
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getNumberValue")
	public AjaxJson getNumberValue(String number) {
		HashMap<String,String> map = mobileSilentService.findAreaNameValueByNumber(number);
		HashMap<String,Object> mapSN = mobileSilentService.findWSValueByAreaIdByNumber(number);
		if(null != map && StringUtils.isNotBlank(map.get("areaId"))){
			//获取排队结果
			Map<String, Object> maps = mobileSilentService.getQueueValue(number, map.get("areaId"));
			if(null != map && StringUtils.isNotBlank(map.get("areaNameValue"))){
				number = number+"("+map.get("areaNameValue")+")";

			}
			maps.put("number",number);
			if(null != mapSN){
				String wino = mobileSilentService.findWSWino(String.valueOf(mapSN.get("areaId")),String.valueOf(mapSN.get("typeNumber")));
				if(StringUtils.isNotBlank(wino) && wino.length() == 1){
					wino = "0"+wino;
				}
				maps.put("result",HttpRequestUtil.shinei(snUrl,String.valueOf(mapSN.get("sign")),mapSN.get("signExpr")+wino));
				if(null != maps.get("result")){
					maps.put("result",JSONObject.fromObject(maps.get("result")).getString("data"));
				}
			}
			return AjaxJson.success("操作成功").put("data",maps);
		}else{
			return  AjaxJson.error("暂无数据");
		}
	}

}
